package com.dailyblue.java;

import lombok.SneakyThrows;

import java.io.*;

// 字符流的读写
public class DemoF {

    private static File file1 = new File("/Users/dailyblue/a/a.txt");
    private static File file2 = new File("/Users/dailyblue/a/b.txt");

    // 不带缓冲的读取
    @SneakyThrows
    private static void a1() {
        Reader reader = new FileReader(file2);
        int one = reader.read();
        System.out.println((char) one);
    }

    @SneakyThrows
    private static void a2() {
        Reader reader = new FileReader(file2);
        char[] chars = new char[10];
        int len = reader.read(chars);
        System.out.println(chars);
    }

    // 带缓冲的读取操作
    @SneakyThrows
    private static void b1() {
        Reader reader = new FileReader(file2);
        BufferedReader br = new BufferedReader(reader);
        String line = br.readLine();
        System.out.println(line);
    }

    @SneakyThrows
    private static void b2() {
        Reader reader = new FileReader(file2);
        BufferedReader br = new BufferedReader(reader);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }

    // 不带缓冲的写入
    @SneakyThrows
    private static void c() {
        Writer writer = new FileWriter(file1, true);
        writer.write("nihao");
        writer.flush();
    }
    // 带缓冲的写入
    @SneakyThrows
    private static void d() {
        Writer writer = new FileWriter(file1, true);
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write("你好");
        bw.flush();
    }
    public static void main(String[] args) {
        d();
    }
}
